package com.lizhanglong.appmanager;

import android.app.ActivityManager;
import android.app.ListActivity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ListActivity {
    private final String TAG = "lzl.appmanager";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_main);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        List<Programe> list = getRunningProcess();
        Log.v(TAG, "size:" + list.size());
        ListAdapter adapter = new ListAdapter(list, this);
        getListView().setAdapter(adapter);
    }

    //正在运行的
    public List<Programe> getRunningProcess() {
        PackagesInfo pi = new PackagesInfo(this);

        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        //获取正在运行的应用
        List<ActivityManager.RunningAppProcessInfo> run = am.getRunningAppProcesses();
        //获取包管理器，在这里主要通过包名获取程序的图标和程序名
        PackageManager pm = this.getPackageManager();
        List<Programe> list = new ArrayList<Programe>();

        for (ActivityManager.RunningAppProcessInfo ra : run) {
            //这里主要是过滤系统的应用和电话应用，当然你也可以把它注释掉。
            if (ra.processName.equals("system") || ra.processName.equals("com.Android.phone")) {
                continue;
            }

            Programe pr = new Programe();
            pr.setIcon(pi.getInfo(ra.processName).loadIcon(pm));
            pr.setName(pi.getInfo(ra.processName).loadLabel(pm).toString());
            System.out.println(pi.getInfo(ra.processName).loadLabel(pm).toString());
            list.add(pr);
        }
        return list;
    }

    public class PackagesInfo {
        private List<ApplicationInfo> appList;

        public PackagesInfo(Context context) {
            //通包管理器，检索所有的应用程序（甚至卸载的）与数据目录
            PackageManager pm = context.getApplicationContext().getPackageManager();
            appList = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
        }


        /**
         * 通过一个程序名返回该程序的一个Application对象。
         *
         * @param name 程序名
         * @return ApplicationInfo
         */

        public ApplicationInfo getInfo(String name) {
            if (name == null) {
                return null;
            }
            for (ApplicationInfo appinfo : appList) {
                if (name.equals(appinfo.processName)) {
                    return appinfo;
                }
            }
            return null;
        }

    }

    public class ListAdapter extends BaseAdapter {
        List<Programe> list = new ArrayList<Programe>();
        LayoutInflater la;
        Context context;

        public ListAdapter(List<Programe> list, Context context) {
            this.list = list;
            this.context = context;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                la = LayoutInflater.from(context);
                convertView = la.inflate(R.layout.list_item, null);

                holder = new ViewHolder();
                holder.imgage = (ImageView) convertView.findViewById(R.id.image);
                holder.text = (TextView) convertView.findViewById(R.id.text);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            final Programe pr = (Programe) list.get(position);
            //设置图标
            holder.imgage.setImageDrawable(pr.getIcon());
            //设置程序名
            holder.text.setText(pr.getName());

            return convertView;
        }
    }

    class ViewHolder {
        TextView text;
        ImageView imgage;
    }

    public class Programe {
        //图标
        private Drawable icon;
        //程序名
        private String name;

        public Drawable getIcon() {
            return icon;
        }

        public void setIcon(Drawable icon) {
            this.icon = icon;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }
}
